package com.itshidu.jiaowu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.service.UserService;
import com.itshidu.jiaowu.util.LoginUtil;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.web.tools.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08
 */

@Primary
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired UserMapper userMapper;

    //登录
    @Override
    public Result login(String username, String password) {
        User user = userMapper.SelectByName(username);
        if (user == null) {
            return Result.of(100, "用户名不存在！！");  //100是用户不存在
        }
        String type=user.getType();
        String pwd=user.getPassword();
        System.out.println(password);
        String str = MD5Util.md5(password+user.getSalt());
        if(pwd.equals(str)){
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            HttpSession session = request.getSession();
            if(type.equals("teacher")){
                System.out.println("teacher");
                session.setAttribute("UserInfo", user);          //把用户信息存进session里
                return Result.of(201, "老师登录");  //201身份是老师
            }
            if(type.equals("student")){
                System.out.println("student");
                session.setAttribute("UserInfo", user);          //把用户信息存进session里
                return Result.of(202, "学生登录");  //100身份是学生
            }
        }
        return Result.of(101, "密码错误！！");
    }

    //注册
    @Override
    public Result register(User user) {
        User a = userMapper.SelectByName(user.getUsername());
        if (a != null) {
            return Result.of(100, "用户名已存在");
        }
        //设置user的初始属性
        user.setType("student");            //默认注册为学生用户
        user.setAvatar("default.png");  //默认头像
        user.setIdcConfirm(2);          //默认未实名
        user.setSalt(UUID.randomUUID().toString()); //设置盐

        //给密码加密
        String m = user.getPassword();
        String s = user.getSalt();
        String password = MD5Util.md5(m + s);
        user.setPassword(password);
        user.setType("student");
        userMapper.insert(user);
        return Result.of(200, "注册成功");

    }

    @Override
    public Result updatepwd(String oldpwd, String newpwd1, String newpwd2) {
        User user=(User) LoginUtil.get("UserInfo");
        String userpwd=user.getPassword();
        String salt=user.getSalt();
        String pwd=MD5Util.md5(oldpwd+salt);
        User u= userMapper.selectById(user.getId());
        if (!userpwd.equals(pwd)){
            return Result.of(200,"密码错误！！");
        }
        if (!newpwd1.equals(newpwd2)){
            return Result.of(100,"新密码不一致！！");
        }
        String newpwd=MD5Util.md5(newpwd1+salt);
        u.setPassword(newpwd);
        userMapper.updateById(u);
        LoginUtil.set("UserInfo",u);
        return Result.of(300,"修改成功");
    }
}
